<?php

	class XMLSQL
	{
		private $queries;

		private $default_params;

		public function __construct( $xmlfile )
		{
			if ( file_exists( $xmlfile ) )
			{
				$xml = simplexml_load_file( $xmlfile );

				foreach( $xml->query AS $cur_query )
					$this->queries[ (string) $cur_query["name"] ] = (string) $cur_query;
			}
			else
				if ( DEV_MODE == "true" )
					error_log( "Unknown XML file: $xmlfile" );
		}

		public function get( $name, $params = array() )
		{
			if ( !$this->queries[$name] )
			{
				if ( DEV_MODE == "true" )
					error_log( "Unknown query: $name - checked file " . $this->xmlfile );

				return false;
			}

			$query = trim( $this->queries[$name] );

			if ( is_array( $params ) )
				foreach( $params AS $key=>$val )
					$query = str_replace( "%$key%", $val, $query );

			if ( $params["order_by"] )
				$query .= " ORDER BY " . $params["order_by"];

			if ( $params["order"] )
				$query .= " ORDER BY " . $params["order"];

			if ( $params["limit"] )
				$query .= " LIMIT " . $params["limit"];

			return $query;
		}
	}

?>

